'''
Created on 29/03/2011

@author: Eran_Z

Scoring
'''

from search_m import searchSingle, searchTogether, searchExclusion, NGD
from util_m import sum

#Helper functions:

def __sigmaMutualWeight(w, context, weights):
    #TODO: DRY with __normalizedSigmaMutualWeight...
    return reduce(sum, map(lambda i:searchTogether(w, context[i])*weights[i], range(len(context))))
    
def __normalizedSigmaMutualWeight(w, context, weights):
    #TODO: inline?
    #TODO: test
    return reduce(sum, map(lambda i:searchTogether(w, context[i])*weights[i]*1.0/searchSingle(w), range(len(context))))
    
########################################################
########################################################
#Main functions:

def basicScorer(context, weights, world):
    #TODO: test
    return map(lambda w:__sigmaMutualWeight(w, context, weights)*1.0/(searchSingle(w)-searchExclusion(w, context)), world)

def NGD1Scorer(context, weights, world):
    #TODO: test
    return 1.0/(reduce(sum, map(lambda i:NGD(world[i], context[i])*weights[i], range(len(context)))))

def NGD2Scorer(context, weights, world):
    #TODO: test
    return reduce(sum, map(lambda i:1.0/(NGD(world[i], context[i])*weights[i]), range(len(context))))

def regularMutualInformationScorer(context, weights, world):
    return map(lambda w:__sigmaMutualWeight(w, context, weights), world)

def normalizedMutualInformationScorer(context, weights, world):
    #TODO: test
    return map(lambda w:__normalizedSigmaMutualWeight(w, context, weights), world)

scoringAlgorithms = {"Basic": basicScorer, "NGD Type 1": NGD1Scorer, "NGD Type 2": NGD2Scorer,
                     "Regular Mutual Information": regularMutualInformationScorer,
                     "Normalized Mutual Information": normalizedMutualInformationScorer }
